package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.CardClassicChangePolicy;
import com.turing.post.cardmanagement.util.CardClassicChangePolicyDao;
import com.turing.post.cardmanagement.util.CardManaApplicationException;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 卡种类变更费率表（CardClassicChangePolicy）
 * 
 * @author zxp
 * @Jan 21, 2013
 */
@Stateless(mappedName = "cardmanagement.impl.CardClassicChangePolicyDaoImpl")
@Remote
@Local
public class CardClassicChangePolicyDaoImpl implements
		CardClassicChangePolicyDao {

//	@EJB(mappedName = "cardmanagement.impl.CardClassicDaoImpl")
//	private CardClassicDao cardClassicDaoImpl;

	@PersistenceContext
	protected EntityManager _em;

	@Override
	public CardClassicChangePolicy getCardClassicChangePolicyByPK(
			CardClassicChangePolicy cccp) {
		return _em.find(CardClassicChangePolicy.class, cccp);
	}

	@Override
	public CardClassicChangePolicy getCardClassicChangePolicyForUpdate(
			CardClassicChangePolicy cccp) {
		String sql = "select * from CardClassicChangePolicy where fromClassicId="
				+ cccp.getFromClassicId()
				+ " and toClassicId="
				+ cccp.getToClassicId() + " for update ";
		Query query = _em.createNativeQuery(sql, CardClassicChangePolicy.class);
		Object obj = query.getSingleResult();
		if (obj != null) {
			return (CardClassicChangePolicy) obj;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardClassicChangePolicy> getCardClassicChangePolicyList(
			BigInteger fromClassicId, BigInteger toClassicId, Integer page) {
		String sql = "select * from CardClassicChangePolicy c where (?1=-1 or fromClassicId=?1) and (?2=-1 or toClassicId=?2) order by c.fromClassicId";
		Query query = _em.createNativeQuery(sql, CardClassicChangePolicy.class);
		query.setParameter(1, fromClassicId);
		query.setParameter(2, toClassicId);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public Integer getCardClassicChangePolicyTotal(BigInteger fromClassicId,
			BigInteger toClassicId) {
		String sql = "select nvl(count(*),0) from CardClassicChangePolicy where (?1=-1 or fromClassicId=?1) and (?2=-1 or toClassicId=?2)";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, fromClassicId);
		query.setParameter(2, toClassicId);
		return Integer.parseInt(query.getSingleResult().toString());
	}

	@Override
	public void insertCardClassicChangePolicy(CardClassicChangePolicy cccp)
			throws PCPSApplicationException {
		if (cccp == null) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"卡种类变更费率对象为空!");
		}

		if (cccp.getFromClassicId().compareTo(cccp.getToClassicId()) == 0) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"不可同类型转换!");
		}

		if (getCardClassicChangePolicyByPK(cccp) != null) {
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"数据库已存在该卡种类变更费率记录!");
		}
		GeneralDao.insert(_em, cccp);
	}

	@Override
	public void deleteCardClassicChangePolicy(CardClassicChangePolicy cccp)
			throws PCPSApplicationException {
		CardClassicChangePolicy _cccp = getCardClassicChangePolicyByPK(cccp);
		if (_cccp != null) {
			GeneralDao.remove(_em, _cccp);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardClassicChangePolicy> getCardClassicChangePolicyListByFromClassicId(
			BigInteger fromClassicId) {
		String sql = "select * from CardClassicChangePolicy c where fromClassicId=?1 order by c.toClassicId";
		Query query = _em.createNativeQuery(sql, CardClassicChangePolicy.class);
		query.setParameter(1, fromClassicId);
		return query.getResultList();
	}
}